<!--
  This file is a part of the open-eBackup project.
  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
  If a copy of the MPL was not distributed with this file, You can obtain one at
  http://mozilla.org/MPL/2.0/.
  
  Copyright (c) [2024] Huawei Technologies Co.,Ltd.
  
  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  -->

<lv-form [formGroup]="formGroup" class="formGroup">
  <lv-form-item>
    <lv-form-label lvRequired>
      {{ 'common_name_label' | i18n }}
    </lv-form-label>
    <lv-form-control [lvErrorTip]="nameErrorTip">
      <input lv-input type="text" formControlName="name" />
    </lv-form-control>
  </lv-form-item>
  <lv-form-item>
    <lv-form-label lvRequired>
      {{ 'common_username_label' | i18n }}
    </lv-form-label>
    <lv-form-control [lvErrorTip]="nameErrorTip">
      <input
        lv-input
        type="text"
        formControlName="username"
        placeholder="{{ 'protection_username_tip_label' | i18n }}"
      />
    </lv-form-control>
  </lv-form-item>
  <lv-form-item>
    <lv-form-label lvRequired>
      {{ 'common_password_label' | i18n }}
      <i
        lv-icon="aui-icon-help"
        [lv-tooltip]="passwordToolTipTpl"
        lvTooltipTheme="light"
        class="configform-constraint"
        lvColorState="true"
      ></i>
    </lv-form-label>
    <lv-form-control [lvErrorTip]="passwordErrorTip">
      <aui-inupt-with-eye
        formControlName="password"
        placeholder="{{ 'protection_password_tip_label' | i18n }}"
      ></aui-inupt-with-eye>
    </lv-form-control>
  </lv-form-item>
  <lv-form-item>
    <lv-form-label lvRequired>
      {{ 'protection_log_archive_checkpoint_interval_label' | i18n }}
      <i
        lv-icon="aui-icon-help"
        lv-tooltip="{{
          'protection_log_archive_checkpoint_interval_tips_label' | i18n
        }}"
        lvTooltipTheme="light"
        class="configform-constraint"
        lvColorState="true"
      ></i>
    </lv-form-label>
    <lv-form-control [lvErrorTip]="rangeErrorTip">
      <lv-group lvGutter="8px" class="lv-group-flex-start">
        <lv-group>
          <input
            lv-input
            type="text"
            formControlName="interval"
            placeholder="{{ '60~120' }}"
            class="backup-for-text"
            style="width: 120px;"
          />
        </lv-group>
        <lv-group class="seconds-label">
          <span>
            {{ 'common_seconds_label' | i18n }}
          </span>
        </lv-group>
      </lv-group>
    </lv-form-control>
  </lv-form-item>
  <lv-form-item>
    <lv-form-label lvRequired>
      {{ 'protection_obclient_host_label' | i18n }}
      <i
        lv-icon="aui-icon-help"
        [lv-tooltip]="obclientTipsTpl"
        lvTooltipTheme="light"
        class="configform-constraint"
        lvColorState="true"
        (mouseenter)="helpHover()"
      ></i>
      <ng-template #obclientTipsTpl>
        <span [innerHTML]="obclientTips"></span>
      </ng-template>
    </lv-form-label>
    <lv-form-control [lvErrorTip]="agentsErrorTip">
      <lv-select
        [lvOptions]="clientProxyOptions"
        formControlName="obClientAgents"
        lvValueKey="value"
        lvShowFilter
        lvShowCheckAll
        lvFilterKey="label"
        lvFilterMode="contains"
        lvMode="multiple"
        [lvContentTemplate]="contentTpl"
      >
      </lv-select>
    </lv-form-control>
  </lv-form-item>

  <div
    class="aui-gutter-column-lg"
    style="flex-direction: column;align-items: normal; margin-top: 20px;"
  >
    <lv-form-label lvRequired>
      {{ 'protection_observer_host_label' | i18n }}
      <i
        lv-icon="aui-icon-help"
        [lv-tooltip]="observerTipsTpl"
        lvTooltipTheme="light"
        class="configform-constraint"
        lvColorState="true"
        (mouseenter)="helpHover()"
      ></i>
      <ng-template #observerTipsTpl>
        <span [innerHTML]="observerTips"></span>
      </ng-template>
    </lv-form-label>
    <lv-datatable [lvData]="[{}]" #lvTable formArrayName="obServerNodes">
      <thead>
        <tr>
          <th>
            {{ 'protection_proxy_host_label' | i18n }}
          </th>
          <th style="width: 200px">{{ 'common_dataplane_ip_label' | i18n }}</th>
          <th style="width: 200px">{{ 'common_port_label' | i18n }}</th>
          <th style="width: 80px;">{{ 'common_operation_label' | i18n }}</th>
        </tr>
      </thead>
      <tbody>
        <ng-container *ngFor="let item of obServerNodes; let i = index">
          <tr [formGroupName]="i">
            <td>
              <lv-form-control class="formControl">
                <lv-select
                  [lvOptions]="proxyOptions"
                  lvAsync
                  formControlName="proxy"
                  lvValueKey="value"
                  lvMode="single"
                  lvShowFilter
                  lvFilterKey="label"
                  lvFilterMode="contains"
                  (ngModelChange)="selectChange()"
                  style="width: 320px;"
                  class="input-position"
                  [lvContentTemplate]="contentTpl"
                >
                </lv-select>
              </lv-form-control>
            </td>
            <td>
              <lv-form-control [lvErrorTip]="ipErrorTip" class="formControl">
                <input
                  lv-input
                  type="text"
                  formControlName="businessIp"
                  class="input-position"
                />
              </lv-form-control>
            </td>
            <td>
              <lv-form-control [lvErrorTip]="portErrorTip" class="formControl">
                <input
                  lv-input
                  type="text"
                  formControlName="port"
                  class="input-position"
                />
              </lv-form-control>
            </td>

            <td>
              <button
                lv-button
                lvType="link"
                (click)="deleteServerRow(i)"
                [disabled]="obServerNodes.length === 1"
              >
                {{ 'common_delete_label' | i18n }}
              </button>
            </td>
          </tr>
        </ng-container>
      </tbody>
    </lv-datatable>

    <button lv-button lvType="link" (click)="addServerRow()">
      <i lv-icon="lv-icon-create"></i>
      <span>{{ 'common_new_add_label' | i18n }}</span>
    </button>
  </div>
</lv-form>
<ng-template #passwordToolTipTpl>
  <ul class="password-tooltip">
    <li>
      {{ 'protection_oceanbase_cluster_password_valid_length_label' | i18n }}
    </li>
    <li>
      {{ 'protection_oceanbase_cluster_password_valid_character_label' | i18n }}
    </li>
    <li>
      {{ 'protection_oceanbase_cluster_password_valid_minlength_label' | i18n }}
    </li>
  </ul>
</ng-template>

<ng-template #contentTpl let-item>
  <lv-group lvGutter="4px">
    <span lv-overflow>{{ item.label }}</span>
    <span class="guide-recommend-label" *ngIf="showGuideNew(item)">
      {{ 'protection_guide_new_resource_label' | i18n }}
    </span>
  </lv-group>
</ng-template>
